package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import dao.impl.BoardDaoImpl;
import dao.impl.QuestionDaoImpl;
import domain.Board;
import domain.Question;

public class InputQuestionAction extends Action {
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Question question = (Question) form;

		int boardId = 1;
		if (request.getParameter("boardId") != null)
			boardId = Integer.parseInt(request.getParameter("boardId"));
		else
			mapping.findForward("forum");
		Board board = new BoardDaoImpl().findById(boardId);
		question.setBoard(board);
		new QuestionDaoImpl().save(question);
		System.out.println(question.getContent());
		// List<Question> list=questionDao.list();
		// request.setAttribute("QuestionList", list);
		String path = request.getContextPath();
    	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    	ActionForward actionForward = new ActionForward(basePath+"forum/forum.jsp?boardId="+boardId,true);
		return actionForward;
//		return mapping.findForward("forum");
	}
}
